//
//  BaseNavigationViewController.swift
//  Exchange
//
//  Created by rui on 2018/4/18.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit


extension UINavigationController {
    
    func customPushViewController(_ viewController: UIViewController, animated: Bool) {
        var controllers = self.viewControllers
        if let nav = self.navigationController as? NavigationController {
            controllers = nav.rui_viewControllers
        }
        let viewControllers =  controllers.filter { (vc) -> Bool in
            return vc.classForCoder == viewController.classForCoder
        }
        if let vc = viewControllers.first {
            self.popToViewController(vc, animated: animated)
        }else {
            self.pushViewController(viewController, animated: animated)
        }
    }
    
    func customReplaceTopPushViewController(_ viewController: UIViewController, animated: Bool) {
        var navigationController = self
        if let navi = self.navigationController {
            navigationController = navi
        }
        let flag = navigationController.viewControllers.count
        self.pushViewController(viewController, animated: true)
        var viewControllers = navigationController.viewControllers
        viewControllers.remove(at: flag - 1)
        navigationController.setViewControllers(viewControllers, animated: false)
    }
    
    func customCheckLoginPushViewController(animated: Bool,_ viewController: () -> UIViewController) {
        if AppInfo.default.isLogin {
            self.pushViewController(viewController(), animated: animated)
        }else {
            let login = LoginViewController()
            self.pushViewController(login, animated: animated)
        }
    }
    
}

////导航栏主题
extension UINavigationController {

    func lightTheme() {
        let image = UIImage.image(color: UIColor(hexStr: "#fdfdfd"))
        self.navigationBar.setBackgroundImage(image, for: UIBarMetrics.default)
        self.navigationBar.shadowImage = image
        self.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor(hexStr: "#393d46")]
        self.navigationBar.tintColor = UIColor(hexStr: "#697183")
        UIApplication.shared.statusBarStyle = .default
    }
    
    func darkTheme() {
        let image = UIImage.image(color: UIColor(hexStr: "#30353f"))
        self.navigationBar.setBackgroundImage(image, for: UIBarMetrics.default)
        self.navigationBar.shadowImage = image
        self.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor(hexStr: "#e7ebf4")]
        self.navigationBar.tintColor = UIColor(hexStr: "#8b9dc1")
        UIApplication.shared.statusBarStyle = .lightContent
    }
    
    func convention()  {
        let image = UIImage.image(color: kTitleColor)
        self.navigationBar.setBackgroundImage(image, for: UIBarMetrics.default)
        self.navigationBar.shadowImage = image
        self.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor:UIColor(hexStr: "#e7ebf4")]
        self.navigationBar.tintColor = UIColor(hexStr: "#e7ebf4")
        UIApplication.shared.statusBarStyle = .lightContent
    }
    
    
    @objc func defaultTheme() {
        if ThemeManager.default.currentThemeMode() == .day {
            lightTheme()
        }else {
            darkTheme()
        }
    }
}




